"""
在线词典 ： 服务端数据处理
"""
from dict_server import *
import pymysql


class Dict:
    def __init__(self):
        self.kwargs = {
            "user": "root",
            "password": "123456",
            "database": "dict",
            "charset": "utf8"
        }
        self.db = pymysql.connect(**self.kwargs)
        self.cur = self.db.cursor()

    def insert_user(self, user, password):
        try:
            sql = "insert into user(user,password) values (%s,%s)"
            self.cur.execute(sql, [user, password])
            self.db.commit()
        except:
            return False
        else:
            return True

    def enter_user(self, user, password):
        sql = "select user,password from user where user=%s and password=%s"
        self.cur.execute(sql, [user, password])
        try:
            data01, data02 = self.cur.fetchone()
            if data01 == user and data02 == password:
                return True
        except:
            return False

    def close(self):
        # 关闭数据库连接
        self.cur.close()
        self.db.close()
